package com.yue.yadmin.service.imp;

import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yue.yadmin.model.BaseModel;
import com.yue.yadmin.service.BaseService;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

public abstract class BaseServiceImpl<T extends BaseModel> implements BaseService<T> {
	
	@Autowired
	protected Mapper<T> mapper;
	
	@Override
	public List<T> getList(T model) {
		if (model != null && model.getPage() != null && model.getRows() != null) {
			PageHelper.startPage(model.getPage(), model.getRows(),model.getOrderBy());
		}
		return mapper.select(model);
	}
	
	@Override
	public T get(T model){
		List<T> list = getList(model);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public PageInfo<T> getPage(T model) {
		PageInfo<T> pageInfo = new PageInfo<T>(getList(model));
		return pageInfo;
	}
	
	@Override
	public int deleteByID(Long id) {
		return mapper.deleteByPrimaryKey(id);
	}
	
	@Override
	public int insert(T model) {
		return mapper.insertSelective(model);
	}
	
	@Override
	public T selectByPrimaryKey(Long id) {
		return mapper.selectByPrimaryKey(id);
	}
	
	@Override
	public int update(T model) {
		if (model.getId() != null) {
			return mapper.updateByPrimaryKeySelective(model);
		}
		else {
			return mapper.insertSelective(model);
		}
	}
	
	@Override
	public List<T> selectBy(String propertyName, Object value) {
		Example example = createExample();
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo(propertyName, value);
		return selectList(example);
	}
	
	@Override
	public T selectOneBy(String propertyName, Object value) {
		List<T> list = selectBy(propertyName, value);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public List<T> selectUniqueBy(String propertyName, Object value) {
		Example example = createExample();
		Criteria criteria = example.createCriteria();
		criteria.andNotEqualTo(propertyName, value);
		return mapper.selectByExample(example);
	}
	
	@Override
	public T selectUniqueOneBy(String propertyName, Object value) {
		List<T> list = selectUniqueBy(propertyName, value);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public List<T> selectList(Criteria criteria) {
		Example example = createExample();
		example.and(criteria);
		return mapper.selectByExample(example);
	}
	
	@Override
	public T selectOne(Criteria criteria) {
		List<T> list = selectList(criteria);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	protected Example createExample() {
		Class<T> c = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		Example example = new Example(c);
		return example;
	}
	
	protected List<T> selectList(Example example) {
		return mapper.selectByExample(example);
	}
	
	protected T selectOne(Example example) {
		List<T> list = selectList(example);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
}
